//
//  VisitorView.swift
//  weibo001
//
//  Created by 李龙 on 15/6/24.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

protocol visitorDelegate: NSObjectProtocol{
    func visitorResgistorClick()
    func visitorLoginClick()
}

class VisitorView: UIView {
    weak var delagate :visitorDelegate?

    func setupViewInfo(iconName: String,messages: String,isHome :Bool )
    {
        message.text=messages
        if isHome
        {
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        }
        else
        {
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            sendSubviewToBack(maskIconView)
        }
    }
    func resgistClick()
    {
        print("1")
        delagate?.visitorResgistorClick()
    }
    func loginClick()
    {
        print("2")
        delagate?.visitorLoginClick()
    }
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(message)
        addSubview(resgister)
        addSubview(login)
        
        ///自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        message.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: message, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant:10))
        addConstraint(NSLayoutConstraint(item: message, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":message]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":message]))
        resgister.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: resgister, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: message, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: resgister, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: message, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":resgister]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":resgister]))
        login.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: message, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: message, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":login]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":login]))
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]-140-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":maskIconView]))
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
    }
    lazy var iconView:UIImageView = {
        let image=UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    lazy var maskIconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    lazy var homeIconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
        }()
    lazy var message:UILabel = {
        let lable = UILabel(frame: CGRectMake(0, 0, 240, 40))
        lable.text="登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(14)
        lable.textAlignment = NSTextAlignment.Center
        lable.preferredMaxLayoutWidth = 224
        lable.numberOfLines=2
        lable.sizeToFit()
        return lable
    }()
    lazy var resgister: UIButton = {
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 40))
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "resgistClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    lazy var login: UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 200, 100, 40))
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
